package com.zyasdk.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import com.zyasdk.enums.AdType;
import com.zyasdk.model.Products;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class LocalUtils {
	/**
	 * 获取本地数据
	 * 
	 * @param type
	 * @return
	 */
	public static String getLocalData(AdType type, Context context) {
		String time = SP.get(context, type.getTimeKey());
		// 如果本地没有保存时间则直接返回空
		if (TextUtils.isEmpty(time))
			return "";
		DateHelper dh = DateHelper.getInstance();
		long mill = dh.getDifference(time, dh.getDataString_1(null));
		// 如果本地保存的数据已经过期则直接返回空
		if (mill > type.getMaxSaveHour() * 60 * 60 * 1000) {
			return "";
		}
		return SP.get(context, type.getDataKey());
	}

	/**
	 * 获取本地产品数据
	 * 
	 * @param type
	 * @param context
	 * @return
	 */
	public static List<Products> getLocalProducts(AdType type, Context context) {
		String data = getLocalData(type, context);
		try {
			return (List<Products>) JSONHelper.parseCollection(data,
					List.class, Products.class);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取本地图片加载完成的数据
	 * 
	 * @param type
	 * @return
	 */
	public static List<Products> getLocalImgLoadData(AdType type,
			Context context) {
		ZYALogUtils.e("load local img data");
		try {
			String data = getLocalData(type, context);
			List<Products> newList = new ArrayList<Products>();
			List<Products> list = (List<Products>) JSONHelper.parseCollection(
					data, List.class, Products.class);
			for (Products pro : list) {
				if (pro.getIsLoadImg() == 1) {
					newList.add(pro);
				}
			}
			return newList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取本地没有下载的产品
	 * 
	 * @param type
	 * @param context
	 * @return
	 */
	@SuppressLint("DefaultLocale")
	public static List<Products> getLocalNotDLData(AdType type, Context context) {
		ZYALogUtils.e("load local not download data");
		try {
			String data = getLocalData(type, context);
			List<Products> newList = new ArrayList<Products>();
			List<Products> list = (List<Products>) JSONHelper.parseCollection(
					data, List.class, Products.class);
			if (list == null || list.size() == 0)
				return null;
			for (Products pro : list) {
				String filepath = pro.getApkfile();
				if (!filepath.toLowerCase().startsWith("http:")) {
					filepath = StaticSetting.FILE_SERVICEPATH + filepath;
				}
				DLUtil dlu = new DLUtil(filepath, null, pro);
				if (!dlu.isFindLocal()) {
					newList.add(pro);
				}
			}
			return newList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 更新本地数据
	 * 
	 * @param type
	 * @param id
	 */
	public static void updateLocalData(AdType type, String id, Context context) {
		String data = getLocalData(type, context);
		try {
			List<Products> list = (List<Products>) JSONHelper.parseCollection(
					data, List.class, Products.class);
			for (Products pro : list) {
				if (pro.getId().equals(id)) {
					pro.setIsLoadImg(1);
					break;
				}
			}
			SP.put(context, type.getDataKey(), JSONHelper.toJSON(list));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static final String LOCALPACKNAME_KEY = "LOCALPACKNAME_KEY";

	/**
	 * 清除本地包名
	 * 
	 * @param context
	 */
	public static void clearLocalPackname(Context context) {
		SP.put(context, LOCALPACKNAME_KEY, "");
	}

	/**
	 * 更新本地保存的包名
	 * 
	 * @param context
	 * @param packageName
	 */
	public static void updateLocalPackname(Context context, String packageName) {
		ZYALogUtils.e("updateLocalPackname:" + packageName);
		String[] localData = getLocalPackname(context);
		if (localData == null) {
			SP.put(context, LOCALPACKNAME_KEY, packageName);
		} else {
			String pns = "";
			for (String data : localData) {
				pns += (pns.equals("") ? data : "," + data);
			}
			pns += (pns.equals("") ? packageName : "," + packageName);
			SP.put(context, LOCALPACKNAME_KEY, pns);
		}
	}

	/**
	 * 获取本地保存包名
	 * 
	 * @param context
	 * @return
	 */
	public static String[] getLocalPackname(Context context) {
		try {
			String data = SP.get(context, LOCALPACKNAME_KEY);
			if (!TextUtils.isEmpty(data)) {
				return data.split(",", -1);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 根据包名获取产品
	 * 
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static Products getProductsByPackName(Context context,
			String packageName) {
		AdType[] types = { AdType.AD, AdType.BANNERS, AdType.BOX,
				AdType.STARTUP };
		for (AdType type : types) {
			List<Products> list = getLocalProducts(type, context);
			if (list == null || list.size() == 0)
				continue;
			for (Products pro : list) {
				if (packageName.equals(pro.getPackName())) {
					return pro;
				}
			}
		}
		return null;
	}
}
